<template>
  <div class="app-container">
    <el-form
      ref="listFilterRef"
      :model="list.filter"
      inline
      class="list-filter"
    >
      <el-form-item prop="name">
        <el-input v-model="list.filter.name" placeholder="券名称" clearable />
      </el-form-item>

      <el-form-item prop="frontStatus">
        <KiSelect
          v-model="list.filter.frontStatus"
          placeholder="券状态"
          :options="frontStatusList"
        />
      </el-form-item>

      <el-form-item>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="table-operation">
      <AuthButton @click="c" name="新增" />
      <el-pagination
        v-bind="elPaginationProps"
        :total="list.total"
        :currentPage.sync="list.filter.pageNo"
        :pageSize.sync="list.filter.pageSize"
      />
    </div>

    <el-table v-loading="list.loading" :data="list.data" v-bind="tableProp">
      <el-table-column align="center" type="index" />
      <el-table-column label="券名称" prop="name" />
      <el-table-column label="券ID" prop="id" />
      <el-table-column label="券状态" prop="frontStatusDesc">
        <template slot-scope="{ row }">
          {{ row | autoFrontStatus(frontStatusList) }}
        </template>
      </el-table-column>
      <el-table-column label="生效时段" prop="useStartTime">
        <template slot-scope="{ row }">
          {{ row.useStartTime }} 至 {{ row.useEndTime }}
        </template>
      </el-table-column>
      <el-table-column label="面值" prop="preferenceNum">
        <template slot-scope="{ row }">
          {{ row.preferenceNum | $cent2yuan }}
        </template>
      </el-table-column>
      <el-table-column label="消费门槛" prop="purchaseAbove">
        <template slot-scope="{ row }">
          {{ row.purchaseAbove | $cent2yuan }}
        </template>
      </el-table-column>
      <el-table-column label="券总数" prop="num" />
      <el-table-column label="使用范围" prop="useRangeType" />
      <!-- ['','全部商品','部分商品'][val] -->
      <el-table-column label="创建时间" prop="createTime" />
      <el-table-column label="创建人" prop="createUserName" />
      <el-table-column align="center" label="操作" width="320">
        <template slot-scope="{ row }">
          <AuthButton @click="r({ id: row.id })" name="查看" />
          <AuthButton @click="$refs.cs.show(row)" name="领券统计" />
          <AuthButton @click="d({ id: row.id })" name="删除" />
        </template>
      </el-table-column>
    </el-table>

    <KiFormDialog
      :show.sync="form.show"
      :title="formTitle"
      v-model="form.data"
      :submit="submitForm"
      :readonly="form.status === 'r'"
      ref="formRef"
      :loading="form.loading"
      width="1200px"
    >
      <template #el-form>
        <el-form-item label="券名称" prop="name" :verify="{ maxLength: 30 }">
          <el-input v-model="form.data.name" placeholder="请输入券名称" />
        </el-form-item>
        <el-form-item
          label="券面值"
          prop="preferenceNum"
          :verify="{ type: 'int', gt: 0 }"
        >
          <PriceInput
            v-model="form.data.preferenceNum"
            placeholder="请输入券面值"
          />
        </el-form-item>
        <el-form-item
          label="券使用门槛"
          prop="purchaseAbove"
          :verify="{ type: 'int', gt: 0 }"
        >
          <PriceInput
            v-model="form.data.purchaseAbove"
            placeholder="请输入券使用门槛"
          />
        </el-form-item>
        <el-form-item
          label="发放数量"
          prop="num"
          :verify="{ type: 'int', gt: 0, lte: 999999 }"
        >
          <el-input v-model="form.data.num" placeholder="请输入发放数量" />
        </el-form-item>
        <el-form-item label="生效时段" prop="useStartTime" verify>
          <TimeRangePicker
            :pickerOptions="pickerOptions"
            type="date-time"
            placeholder="请选择"
            :start.sync="form.data.useStartTime"
            :end.sync="form.data.useEndTime"
          />
        </el-form-item>
        <el-form-item
          label="可领取次数"
          prop="personLimit"
          :verify="{ gte: 0, lte: 100000 }"
        >
          <el-input
            v-model="form.data.personLimit"
            placeholder="请输入可领取次数"
          />
        </el-form-item>
        <el-form-item label="券退款设置" prop="refundCoupon" verify>
          <KiSelect
            v-model="form.data.refundCoupon"
            :options="refundCouponList"
            disabled
          />
        </el-form-item>
        <el-form-item label="券使用范围" prop="useRangeType" verify>
          <KiSelect
            v-model="form.data.useRangeType"
            :options="useRangeTypeList"
          />
        </el-form-item>
        <el-form-item
          label="选择商品"
          prop="productIds"
          verify
          v-if="form.data.useRangeType == 2"
        >
          <BoundGoods
            v-if="form.show"
            v-model="form.data.productIds"
            :type="form.status"
            :disabled="form.status === 'r'"
          />
        </el-form-item>
        <el-form-item label="备注说明" prop="remark">
          <el-input
            type="textarea"
            :rows="3"
            v-model="form.data.remark"
            maxlength="100"
            show-word-limit
            placeholder="请输入备注"
          ></el-input>
        </el-form-item>
      </template>
    </KiFormDialog>

    <coupon-statistics ref="cs" />
  </div>
</template>

<script>
import pageMixin from '@/utils/pageMixin'
import useAdmateAdapter from '@/utils/useAdmateAdapter'
import BoundGoods from '@/components/BoundGoods'
import couponStatistics from './components/coupon-statistics'
export default {
  mixins: [pageMixin],
  components: { BoundGoods, couponStatistics },
  setup: () =>
    useAdmateAdapter({
      urlPrefix: 'sot-admin-api/product/full-coupon',
      form: {
        data: {
          refundCoupon: 1
        }
      }
    }),
  data () {
    return {
      pickerOptions: {
        // 时间限制
        startDisabledDate (time, start, end) {
          let date = new Date()
          let othe = new Date(time)
          if (othe.getTime() < date.getTime() - 24 * 60 * 60 * 1000) {
            return true
          }
          return false
        }
      },
      // 券退款设置(退款退券)
      refundCouponList: [
        { dataName: '退款退券', dataValue: 1 },
        { dataName: '否', dataValue: 0 }
      ],
      // 券使用范围
      useRangeTypeList: [
        { dataName: '全部商品', dataValue: 1 },
        { dataName: '部分商品', dataValue: 2 }
      ],
      frontStatusList: [
        { dataName: '未开始', dataValue: 0, dataType: undefined },
        { dataName: '生效中', dataValue: 1, dataType: 'success' },
        { dataName: '已过期', dataValue: 2, dataType: 'danger' }
      ]
    }
  },
  filters: {
    autoFrontStatus (row, frontStatusList) {
      // 未开始
      let nowDate = new Date().getTime()
      if (nowDate < new Date(row.useStartTime).getTime()) {
        return frontStatusList[0].dataName
      }
      // 已过期
      else if (nowDate > new Date(row.useEndTime).getTime()) {
        return frontStatusList[2].dataName
      } else {
        return frontStatusList[1].dataName
      }
    }
  }
}
</script>
